import 'nprogress/nprogress.css';
import nProgress from 'nprogress';
import axios, { type AxiosResponse, type InternalAxiosRequestConfig } from 'axios';

const request = axios.create({
  baseURL: import.meta.env.VITE_BASE_URL,
  timeout: 5000
});

request.interceptors.request.use(
  (config: InternalAxiosRequestConfig<any>): InternalAxiosRequestConfig<any> => {
    nProgress.start();
    return config;
  },
  (err: any): Promise<any> => {
    nProgress.done();
    return Promise.reject(err);
  }
);

request.interceptors.response.use(
  (res: AxiosResponse<any, any>): AxiosResponse<any, any> => {
    nProgress.done();
    return res.data;
  },
  (err: any): Promise<any> => {
    nProgress.done();
    return Promise.reject(err);
  }
);

export default request;
